package com.iot.ic.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.ManualIntervention;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.service.ManualInterventionService;
import com.iot.ic.service.RefereeService;
import com.iot.ic.service.TeamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "人工干预信息相关接口")
@RestController
public class ManualInterventionController extends BaseController {
	
	@Autowired
	private ManualInterventionService manualInterventionService;

	@Autowired
	SimpMessagingTemplate simpMessagingTemplate;
	
	
	@ApiOperation(value = "获取最近count条场景得分记录", notes = "count是获取条数，不带此参数可以获取全部记录。teamId是参赛队Id，不带此参数可以获取全部队伍的场景得分")
    @GetMapping(value = "/manualInterventions")
    public Response getScenesResultList(@RequestParam(value = "count", required = false, defaultValue = "99999") Integer count,
                                        @RequestParam(value = "teamId", required = false, defaultValue = "-1") Integer teamId,
                                        @RequestParam(value = "startTime", required = false) String startTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        Response response = new Response();
        List<Map<String, Object>> list = manualInterventionService.listManualInterventionByCount(count, teamId, startTime, endTime);
        response.setResult(list);
        return response;
    }

	@ApiOperation(value = "获取最近count条场景得分记录，websocket请求", notes = "count是获取条数，不带此参数可以获取全部记录。teamId是参赛队Id，不带此参数可以获取全部队伍的场景得分")
	@MessageMapping("/manualInterventionsSocket")
	@SendToUser("/manual/interventions")
	public void getScenesResultListSocket(
			@Header(value = "user", required = false, defaultValue = "") String adminId,
			@Header(value = "count", required = false, defaultValue = "99999") Integer count,
			@Header(value = "teamId", required = false, defaultValue = "-1") Integer teamId,
			@Header(value = "startTime", required = false) String startTime,
			@Header(value = "endTime", required = false) String endTime)
	{
		Response response = new Response();
		List<Map<String, Object>> list = manualInterventionService.listManualInterventionByCount(count, teamId, startTime, endTime);
		response.setResult(list);

		simpMessagingTemplate.convertAndSendToUser(adminId, "/manual/interventions", response);
	}
	
	/**
	 * 开始人工干预
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="开始人工干预", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/manualIntervention/start", method=RequestMethod.POST)
	public Response startManualIntervention(@PathVariable("userId") int userId,
			@Valid@RequestBody ManualIntervention manualIntervention){
		Response response = new Response();
		int result = manualInterventionService.startManualIntervention(manualIntervention);
		if (result > 0) {
            response.setInfos(ReturnCode.No_StartManualInterventionOK);
            response.setResult(result);
        } else {
            response.setInfos(ReturnCode.Err_StartManualInterventionFailor);
        }
		return response;
	}
	
	/**
	 * 结束人工干预
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="结束人工干预", notes="")
	@RequestMapping(value="/users/{userId}/manualIntervention/{manualInterveneId}/end", method=RequestMethod.PUT)
	public Response endManualIntervention(@PathVariable("userId") int userId,
			@PathVariable("manualInterveneId") int manualInterveneId){
		Response response = new Response();
		ManualIntervention manualIntervention = new ManualIntervention();
		manualIntervention.setManualInterveneId(manualInterveneId);
		manualIntervention.setEndTime(new Date());
		
		int result = manualInterventionService.endManualIntervention(manualIntervention);
		if (result > 0) {
            response.setInfos(ReturnCode.No_EndManualInterventionOK);
        } else {
            response.setInfos(ReturnCode.Err_EndManualInterventionFailor);
        }
		return response;
	}
	
	/**
	 * 根据场景获得人工干预结果
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="根据场景获得人工干预结果", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/scenesResult/{scenesResultId}/manualIntervention", method=RequestMethod.GET)
	public Response getManualInterventionByScene(@PathVariable("userId") int userId,
			@PathVariable("scenesResultId") int scenesResultId){
		Response response = new Response();
		List<ManualIntervention> manualInterventionList = manualInterventionService.listManualInterventionByScenes(scenesResultId);
		response.setResult(manualInterventionList);
		return response;
	}
	
	@ApiOperation(value="删除人工干预", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/manualIntervention/{manualInterventionId}", method=RequestMethod.DELETE)
	public Response deleteManualIntervention(@PathVariable("userId") int userId,
			@PathVariable("manualInterventionId") int manualInterventionId){
		Response response = new Response();
		int result = manualInterventionService.deleteManualIntervention(manualInterventionId);
		if (result > 0) {
            response.setInfos(ReturnCode.No_DeleteManualInterventionOK);
        } else {
            response.setInfos(ReturnCode.Err_DeleteManualInterventionFailor);
        }
		return response;
	}

	@ApiOperation(value="web端删除人工干预", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/manualInterventions/{manualInterventionId}", method=RequestMethod.DELETE)
	public Response deleteManualInterventionAfter(@PathVariable("userId") int userId,
											 @PathVariable("manualInterventionId") int manualInterventionId){
		Response response = new Response();
		int result = manualInterventionService.deleteManualInterventionAfter(manualInterventionId);
		if (result > 0) {
			response.setInfos(ReturnCode.No_DeleteManualInterventionOK);
			response.setResult(result);
		} else {
			response.setInfos(ReturnCode.Err_DeleteManualInterventionFailor);
		}
		return response;
	}
	
}
